﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DriveBeamEditor.Properties;

namespace DriveBeamEditor.Controls.General.Popup
{
    public partial class PopupContainer : UserControl
    {
        #region Fields

        private string _text = "PopupContainer";
        private Color _foreColor = Color.Black;
        private Color _backColor2 = Color.Gainsboro;

        public event EventHandler ClosePopup;

        #endregion

        #region Properties

        [Category("Appearance")]
        public Color BackColor2
        {
            get { return _backColor2; }
            set
            {
                _backColor2 = value;
                Invalidate();
            }
        }

        [Browsable(true)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get { return _text; }
            set
            {
                _text = value;
                Invalidate();
            }
        }

        [Browsable(true)]
        public override Color ForeColor
        {
            get { return _foreColor; }
            set
            {
                _foreColor = value;
                Invalidate();
            }
        }

        #endregion

        #region CTORs

        public PopupContainer()
        {
            InitializeComponent();
            SetStyle(
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.DoubleBuffer |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.ResizeRedraw |
                ControlStyles.CacheText |
                ControlStyles.SupportsTransparentBackColor |
                ControlStyles.UserPaint,
                true
            );
        }

        #endregion

        #region Overrides

        protected override void OnPaint(PaintEventArgs e)
        {
            var rect = new RectangleF(0, Resources.beamng.Height, Width, Height - Resources.beamng.Height);
            var botRect = new RectangleF(0, Height - Resources.beamng.Height, Width, Resources.beamng.Height);
            var topRect = new RectangleF(0, 0, Width, Resources.beamng.Height + 1);
            var bordRect = new RectangleF(
                Padding.Left,
                Padding.Top + Resources.beamng.Height,
                Width - Padding.Left - Padding.Right - 1,
                Height - Padding.Top - Padding.Bottom - Resources.beamng.Height * 2 - 1
            );
            var tSize = e.Graphics.MeasureString(Text, Font);
            var sColor = Color.FromArgb(120, 255 - ForeColor.R, 255 - ForeColor.G, 255 - ForeColor.B);

            e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            e.Graphics.FillRectangle(new LinearGradientBrush(rect, BackColor2, BackColor, LinearGradientMode.Vertical), rect);
            e.Graphics.FillRectangle(new LinearGradientBrush(botRect, BackColor2, Color.FromArgb(255, 102, 0), LinearGradientMode.Vertical), botRect);
            e.Graphics.FillRectangle(new LinearGradientBrush(topRect, Color.FromArgb(150, 255, 102, 0), Color.Transparent, LinearGradientMode.Vertical), topRect);
            e.Graphics.DrawString(Text, Font, new SolidBrush(sColor), Resources.beamng.Width + 2f, topRect.Height / 2f - tSize.Height / 2f + 1f);
            e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), Resources.beamng.Width, topRect.Height / 2f - tSize.Height / 2f);
            e.Graphics.DrawRectangle(new Pen(Color.FromArgb(255, 108, 0)), bordRect.X, bordRect.Y, bordRect.Width, bordRect.Height);

            base.OnPaint(e);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if ((keyData & Keys.Alt) == Keys.Alt && (keyData & Keys.F4) == Keys.F4)
            {
                Parent.Hide();
                return true;
            }

            if ((keyData & Keys.Enter) == Keys.Enter && ActiveControl is Button)
            {
                (ActiveControl as Button).PerformClick();
                return true;
            }

            return base.ProcessDialogKey(keyData);
        }

        #endregion

        #region Methods

        public virtual void Close()
        {
            if (ClosePopup != null)
                ClosePopup(this, new EventArgs());
        }

        #endregion
    }
}
